package com.ruoyi.supplier.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supplier.domain.TbStoreOut;
import com.ruoyi.supplier.service.ITbStoreOutService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出库Controller
 * 
 * @author ruoyi
 * @date 2021-03-07
 */
@RestController
@RequestMapping("/supplier/storeOut")
public class TbStoreOutController extends BaseController
{
    @Autowired
    private ITbStoreOutService tbStoreOutService;

    /**
     * 查询出库列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:storeOut:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbStoreOut tbStoreOut)
    {
        startPage();
        List<TbStoreOut> list = tbStoreOutService.selectTbStoreOutList(tbStoreOut);
        return getDataTable(list);
    }

    /**
     * 导出出库列表
     */
    @PreAuthorize("@ss.hasPermi('supplier:storeOut:export')")
    @Log(title = "出库", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TbStoreOut tbStoreOut)
    {
        List<TbStoreOut> list = tbStoreOutService.selectTbStoreOutList(tbStoreOut);
        ExcelUtil<TbStoreOut> util = new ExcelUtil<TbStoreOut>(TbStoreOut.class);
        return util.exportExcel(list, "storeOut");
    }

    /**
     * 获取出库详细信息
     */
    @PreAuthorize("@ss.hasPermi('supplier:storeOut:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(tbStoreOutService.selectTbStoreOutById(id));
    }

    /**
     * 新增出库
     */
    @PreAuthorize("@ss.hasPermi('supplier:storeOut:add')")
    @Log(title = "出库", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbStoreOut tbStoreOut)
    {
        // 如果状态等于 1 表示 需要将商品出库及修改生产日期
        if(tbStoreOut.getStoreStatus()==1){
            return toAjax(tbStoreOutService.insertTbStoreOutQty(tbStoreOut));
        }
        return toAjax(tbStoreOutService.insertTbStoreOut(tbStoreOut));
    }

    /**
     * 修改出库
     */
    @PreAuthorize("@ss.hasPermi('supplier:storeOut:edit')")
    @Log(title = "出库", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbStoreOut tbStoreOut)
    {
        // 如果状态等于 1 表示 需要将商品出库及修改生产日期
        if(tbStoreOut.getStoreStatus()==1){
            return toAjax(tbStoreOutService.updateTbStoreOutQty(tbStoreOut));
        }
        return toAjax(tbStoreOutService.updateTbStoreOut(tbStoreOut));
    }

    /**
     * 删除出库
     */
    @PreAuthorize("@ss.hasPermi('supplier:storeOut:remove')")
    @Log(title = "出库", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbStoreOutService.deleteTbStoreOutByIds(ids));
    }
}
